<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport"
    content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  <meta name="author" content="火星科技 http://mars2d.cn " />
  <meta name="apple-touch-fullscreen" content="yes" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  <meta name="format-detection" content="telephone=no" />
  <meta name="x5-fullscreen" content="true" />
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  <!-- 标题及搜索关键字 -->
  <meta name="keywords" content="火星科技,cesium,2D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  <meta name="description"
    content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS" />

  <link rel="shortcut icon" type="image/x-icon" href="img/favicon/favicon.ico" />
  <title>高德路径规划查询 | Mars2d | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

  <!--第三方lib-->
  <script type="text/javascript" src="/lib/include-lib.js" libpath="/lib/"
    include="jquery,font-awesome,bootstrap,layer,haoutil,toastr,mars2d"></script>

  <link href="/css/style.css" rel="stylesheet" />
</head>
<style>
  .infoview span {
    color: #000 !important;
  }
</style>

<body>


  <div id="mars2dContainer" class="mars2d-container"></div>

  <div class="infoview">
    <table class="mars-table" style="border-collapse:separate; border-spacing:0px 6px;">
      <tr>
        <td>方式</td>
        <td colspan="2">
          <select id="queryType" class="selectpicker form-control">
            <option value="1" selected="selected">步行路线查询</option>
            <!-- <option value="2">骑行路线查询</option> -->
            <option value="3">驾车路线查询</option>
          </select>
        </td>
      </tr>
      <tr>
        <td>起点</td>
        <td>
          <input id="startIpt" type="text" value="" class="form-control" style="width: 200px" />
        </td>
        <td>
          <input type="button" class="btn btn-primary" value="选点" id="startPoint" />
        </td>
      </tr>
      <tr>
        <td>终点：</td>
        <td>
          <input id="endIpt" type="text" value="" class="form-control" style="width: 200px" />
        </td>
        <td>
          <input type="button" class="btn btn-primary" value="选点" id="endPoint" />
        </td>
      </tr>
      <tr>
        <td></td>
        <td colspan="3">
          <input type="button" class="btn btn-primary" value="开始分析" id="FX" />
          <input type="button" class="btn btn-primary" value="清除" id="clear" />
        </td>
      </tr>
    </table>
  </div>

  <div class="infoview result" style="top: 250px; display: none; word-break: break-all; width: 300px">
    总距离：<span id="allDiatance" style="color: #cad1d1"></span><br />
    预计时间：<span id="useTime" style="color: #cad1d1"></span><br />
    导航：<span id="dh" style="color: #cad1d1"></span><br />
  </div>

  <script src="/js/common.js"></script>
  <script src="./map.js"></script>

  <script type="text/javascript">
    "use script" //开发环境建议开启严格模式
    let routeLayer
    let gaodeRoute

    //当前页面业务相关
    let startGraphic, endGraphic

    let lineGraphic

    var map

    function initUI(options) {
      //合并属性参数，可覆盖config.json中的对应配置
      let mapOptions = mars2d.Util.merge(options, {
        center: { lat: 31.797919, lng: 117.281329, alt: 36236, heading: 358, pitch: -81 }
      })

      //创建矢量数据图层
      routeLayer = new mars2d.layer.GraphicLayer()
      map.addLayer(routeLayer)

      gaodeRoute = new mars2d.query.GaodeRoute({
        // key: ['ae29a37307840c7ae4a785ac905927e0'],
      })

      $("#startPoint").click(function () {
        if (startGraphic) {
          startGraphic.remove()
          startGraphic = null
        }

        map.graphicLayer.startDraw({
          type: "marker",
          style: {
            image: "img/marker/start.png"
            // horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            // verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          },
          success: function (graphic) {
            startGraphic = graphic

            let point = graphic.latlng
            // point.format();
            $("#startIpt").val([point.lat, point.lng])
            console.log(point)
            queryRoute()
          }
        })
      })
      $("#endPoint").click(function () {
        if (endGraphic) {
          endGraphic.remove()
          endGraphic = null
        }

        map.graphicLayer.startDraw({
          type: "marker",
          style: {
            image: "img/marker/end.png"
            // horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            // verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
          },
          success: function (graphic) {
            endGraphic = graphic

            let point = graphic.latlng
            // point.format();
            $("#endIpt").val([point.lat, point.lng])

            queryRoute()
          }
        })
      })

      //开始分析
      $("#FX").click(function () {
        if (!startGraphic || !endGraphic) {
          haoutil.msg("请设置起点和终点")
          return
        }
        queryRoute()
      })

      $("#clear").click(function () {
        if (startGraphic) {
          startGraphic.remove()
          startGraphic = null
        }
        if (endGraphic) {
          endGraphic.remove()
          endGraphic = null
        }
        $("#startIpt").val("")
        $("#endIpt").val("")

        routeLayer.clear()
        $(".result").hide()
      })
    }

    function queryRoute() {
      if (!startGraphic || !endGraphic) {
        return
      }

      routeLayer.clear()

      haoutil.loading.show()
      $(".result").hide()

      let type = Number($("#queryType").val()) //GaodeRouteType枚举类型
      console.log(startGraphic)
      gaodeRoute.query({
        type: type,
        points: [startGraphic.coordinates[0], endGraphic.coordinates[0]],
        success: function (data) {
          console.log(data)
          haoutil.loading.close()

          let lineFirst = data.paths[0]
          let points = lineFirst.points
          if (!points || points.length < 1) {
            return
          }

          let time = haoutil.str.formatTime(lineFirst.allDuration)
          let distance = haoutil.str.formatLength(lineFirst.allDistance)
          let html = "<div>总距离：" + distance + "<br/>所需时间：" + time + "</div>"
          const latlngs = mars2d.PointTrans.coords2latlngs(points)
          let graphic = new mars2d.graphic.Polyline({
            latlngs: latlngs,
            style: {
              clampToGround: true,
              // material: Cesium.Color.AQUA.withAlpha(0.8),
              width: 5
            },
            popup: html
          })
          routeLayer.addGraphic(graphic)

          showResult(data)
        },
        error: function (msg) {
          haoutil.loading.close()

          window.toastr.error(msg)
        }
      })
    }

    function showResult(data) {
      $(".result").show()
      console.log(data)
      let allTime = haoutil.str.formatTime(data.paths[0].allDuration)
      let allDistance = haoutil.str.formatLength(data.paths[0].allDistance)

      let dhHtml = ""
      for (let i = 0; i < data.paths[0].steps.length; i++) {
        let item = data.paths[0].steps[i]
        dhHtml += item.instruction + "；"
      }
      $("#useTime").html(allTime)

      $("#allDiatance").html(allDistance)
      $("#dh").html(dhHtml)
    }
  </script>
</body>

</html>
